package com.coding.kan_yi_dao.interceptor;


import com.coding.kan_yi_dao.entity.User;
import com.coding.kan_yi_dao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// 并不是拦截所有请求（个人中心、支付、加入购物车、查看订单 需要登录校验）
// 控制器 ： 如何获取请求内部的数据？ public Result test(String name){return }
@Component
public class LoginValidateInterceptor implements HandlerInterceptor {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1、ThreadLocal当中获取数据
        User user = UserHolder.get();
        // 2、为空，返回401
        if(user == null){
            response.setStatus(401);
            return false;
        }
        // 3、直接放行
        return true;
    }
}
